//
//  DaibanCell.swift
//  ZYA
//
//  Created by 邱仙凯 on 2024/12/23.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit
 
class DaibanCell: BadgeCollectionViewCell {
    
    var itemDarr = [MenuItem]() {
        didSet {
            for i in 0..<btnArr.count {
                
                let btn = btnArr[i]
                
                if itemDarr.count <= 3 {
                    btn.snp.updateConstraints { (make) in
                        make.height.equalTo(self.ks_height - padding)
                    }
                }
                
                if itemDarr.count > i {
                    btn.isHidden = false
                    let ttS = itemDarr[i].title.unwrap
                    
                    
                    let tapT = UITapGestureRecognizer()
                    tapT.numberOfTapsRequired = 1
                    tapT.addTap { [unowned self,ttS] ges in
                        
                        if let clo = self.ddclo {
                            clo(itemDarr[i])
                        }
                    }
                    btn.addGestureRecognizer(tapT)
                    
                    
                    
                    btn.title.text = ttS
                    btn.img.image = UIImage(named: itemDarr[i].imgStr.unwrap)
                    
                    if ttS == "我的审批" {
                        if BadgeUtil.shared.auditNum.noaudit.intWrap > 0 {
                            btn.num.text = BadgeUtil.shared.auditNum.noaudit.unwrap
                        } else {
                            btn.num.text = "0"
                        }
                        
                    } else if ttS == "已审" {
                        if BadgeUtil.shared.auditNum.audited.intWrap > 0 {
                            btn.num.text = BadgeUtil.shared.auditNum.audited.unwrap
                        } else {
                            btn.num.text = "0"
                        }
                    } else if ttS == "已退" {
                        if BadgeUtil.shared.auditNum.refuse.intWrap > 0 {
                            btn.num.text = BadgeUtil.shared.auditNum.refuse.unwrap
                        } else {
                            btn.num.text = "0"
                        }
                    } else if ttS == "请假审批" {
                        if BadgeUtil.shared.auditNum.leave.intWrap > 0 {
                            btn.num.text = BadgeUtil.shared.auditNum.leave.unwrap
                        } else {
                            btn.num.text = "0"
                    }
                    } else if ttS == "采购付款审批" {
                        
                        if BadgeUtil.shared.auditNum.cgpay.intWrap > 0 {
                            btn.num.text = BadgeUtil.shared.auditNum.cgpay.unwrap
                        } else {
                            btn.num.text = "0"
                        }
                        
                    } else if ttS == "付款确认" {
                        if BadgeUtil.shared.auditNum.paysure.intWrap > 0 {
                            btn.num.text = BadgeUtil.shared.auditNum.paysure.unwrap
                        } else {
                            btn.num.text = "0"
                        }
                    } else if ttS == "知会提醒" {
                        
                        if BadgeUtil.shared.auditNum.notice.intWrap > 0 {
                            btn.num.text = BadgeUtil.shared.auditNum.notice.unwrap
                        } else {
                            btn.num.text = "0"
                        }
                        
                    } else if ttS == "养殖销售合同" {
                        
                        if BadgeUtil.shared.auditNum.yzht.intWrap > 0 {
                            btn.num.text = BadgeUtil.shared.auditNum.yzht.unwrap
                        } else {
                            btn.num.text = "0"
                        }
                        
                    } else if ttS == "原料采购合同" {
                        
                        if BadgeUtil.shared.auditNum.cght.intWrap > 0 {
                            btn.num.text = BadgeUtil.shared.auditNum.cght.unwrap
                        } else {
                            btn.num.text = "0"
                        }
                        
                    } else if ttS == "客户信息审批" {
                        
                        if BadgeUtil.shared.auditNum.noClient.intWrap > 0 {
                            btn.num.text = BadgeUtil.shared.auditNum.noClient.unwrap
                        } else {
                            btn.num.text = "0"
                        }
                        
                    } else if ttS == "销售合同审批" {
                        
                        if BadgeUtil.shared.auditNum.xsht.intWrap > 0 {
                            btn.num.text = BadgeUtil.shared.auditNum.xsht.unwrap
                        } else {
                            btn.num.text = "0"
                        }
                        
                    } else if ttS == "合(折)财务审批" {
                        
                        if BadgeUtil.shared.auditNum.hzcwaudit.intWrap > 0 {
                            btn.num.text = BadgeUtil.shared.auditNum.hzcwaudit.unwrap
                        } else {
                            btn.num.text = "0"
                        }
                        
                    } else if ttS == "过磅差值工厂" {
                        
                        if BadgeUtil.shared.auditNum.gbcz_fac.intWrap > 0 {
                            btn.num.text = BadgeUtil.shared.auditNum.gbcz_fac.unwrap
                        } else {
                            btn.num.text = "0"
                    }
                        
                    } else if ttS == "过磅差值财务" {
                        
                        if BadgeUtil.shared.auditNum.gbcz_cw.intWrap > 0 {
                            btn.num.text = BadgeUtil.shared.auditNum.gbcz_cw.unwrap
                        } else {
                            btn.num.text = "0"
                        }
                        
                    } else if ttS == "保证金审核" {
                        if BadgeUtil.shared.auditNum.bzjin.intWrap > 0 {
                            btn.num.text = BadgeUtil.shared.auditNum.bzjin.unwrap
                        } else {
                            btn.num.text = "0"
                        }
                    } else if ttS == "抄送" {
                        if BadgeUtil.shared.auditNum.caoSong.intWrap > 0 {
                            btn.num.text = BadgeUtil.shared.auditNum.caoSong.unwrap
                        } else {
                            btn.num.text = "0"
                        }
                    } else {
                        btn.num.text = "0"
                        
                    }
 
                } else {
                    btn.isHidden = true
                }
            
            }
        }
    }
    var btnArr = [DaibanSView]()
    convenience required init(coder : NSCoder){
        self.init()
    }

    var ddclo:((MenuItem)->())?
    
    private var cellH: CGFloat = 0
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        for _ in 0..<6 {
            let v = DaibanSView()
          
            btnArr.append(v)
            self.contentView.addSubview(v)
        }
         
        // 审核的按钮布局
        let padding = 8
        let ww = (Int(self.ks_width) - padding * 4)/3
        cellH = CGFloat((Int(self.ks_height) - padding)/2)
        for i in 0..<btnArr.count {
            
            let lie = i % 3
            let hang = i / 3
            let btn = btnArr[i]
         
            let leftPadding = (lie + 1) * padding + lie * ww
            let topPadding = (hang) * padding + hang * Int(cellH)
            btn.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(leftPadding)
                make.height.equalTo(cellH)
                make.width.equalTo(ww)
                make.top.equalToSuperview().offset(topPadding)
            }
        }
        
    }
 
 
}
class DaibanSView:UIView {
    var img: UIImageView = {
        let img = UIImageView()
        return img
    }()
    var title: UILabel = {
        let lab :UILabel = UILabel()
        lab.textColor = UIColor(kshex:"#8991B0")
        lab.font = UIFont.systemFont(ofSize: 13)
        lab.textAlignment = .center
        lab.numberOfLines = 2
        return lab
    }()
    var num: UILabel = {
        let lab :UILabel = UILabel()
        lab.textColor = UIColor(kshex:"#222222")
        lab.font = UIFont.boldSystemFont(ofSize: 19)
        lab.textAlignment = .center
        lab.numberOfLines = 2
        lab.text = "0"
        return lab
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isUserInteractionEnabled = true
     
             self.backgroundColor = UIColor(kshex: "#EDF5FF")
            self.layer.cornerRadius = 5

            self.addSubview(img)
            self.addSubview(title)
            self.addSubview(num)
            img.snp.makeConstraints { (make) in
                make.right.equalTo(self).offset(-10)
                make.bottom.equalTo(self).offset(-8)
                make.height.equalToSuperview().multipliedBy(0.4)
                make.width.equalTo(img.snp.height)

            }
           
            title.snp.makeConstraints { (make) in
                make.left.equalTo(self).offset(8)
                make.top.equalTo(self).offset(8)
            }
            num.snp.makeConstraints { (make) in
                make.left.equalTo(title)
                make.bottom.equalTo(img)
            }
            
      
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


